package com.zhh.srforum.controller;

import com.zhh.srforum.cache.TagCache;
import com.zhh.srforum.dto.QuestionDTO;
import com.zhh.srforum.mapper.QuestionMapper;
import com.zhh.srforum.mapper.UserMapper;
import com.zhh.srforum.model.Question;
import com.zhh.srforum.model.User;
import com.zhh.srforum.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.WebParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.awt.im.InputMethodHighlight;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Zhao Haihang
 * @Date: 2020/11/11/20:34
 * @Description:
 */
@Controller
public class PublishController {

    @Autowired
    private QuestionService questionService;

    @GetMapping("/publish")
    public String publish(Model model){
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }

    @GetMapping("/publish/{id}")
    public String  edit(@PathVariable("id") Long id, Model model){

        QuestionDTO questionDTO = questionService.findById(id);
        model.addAttribute("title",questionDTO.getTitle());
        model.addAttribute("description", questionDTO.getDescription());
        model.addAttribute("tag", questionDTO.getTag());
        model.addAttribute("id", questionDTO.getId());
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }

    @PostMapping("/publish")
    public String doPublish(
      @RequestParam("title") String title,
      @RequestParam("description") String description,
      @RequestParam("tag") String tag,
      @RequestParam(value = "id") Long id,
      HttpServletRequest httpServletRequest,
      Model model
    ){
        //回显
        model.addAttribute("title",title);
        model.addAttribute("description", description);
        model.addAttribute("tag", tag);
        model.addAttribute("tags", TagCache.get());

        //校验
        if (StringUtils.isBlank(title)) {
            model.addAttribute("error", "标题不能为空");
            return "publish";
        }
        if (StringUtils.isBlank(description)) {
            model.addAttribute("error", "问题补充不能为空");
            return "publish";
        }
        if (StringUtils.isBlank(tag)) {
            model.addAttribute("error", "标签不能为空");
            return "publish";
        }

        String invalid = TagCache.filterInvalid(tag);
        if (StringUtils.isNotBlank(invalid)) {
            model.addAttribute("error", "输入非法标签:" + invalid);
            return "publish";
        }

        //获取user
        User user= (User)httpServletRequest.getSession().getAttribute("user");
        if(user==null){
            model.addAttribute("error", "用户没有登录");
            return "publish";
        }

        //封装问题
        Question question = new Question();
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setCreator(user.getId());
        question.setId(id);

        questionService.createOrUpdate(question);

        return "redirect:";
    }



}
